<?php
session_start();
include_once("conn.php");
$username = $_POST["username"];
$password = $_POST["password"];

$sql = "select * from bbs_user where u_name = '".$username."' && u_password = '".$password."'";
$result = mysql_query($sql);
$rs = mysql_fetch_array($result);

if($rs["u_name"])
{
	$_SESSION["current_user"] = $rs["u_name"];
	date_default_timezone_set("PRC");
	$time = date("Y-m-d H:i:s",time());
	$ip = $_SERVER["REMOTE_ADDR"];
	$query2 = "select u_lastlogintime from bbs_user where u_name = '".$username."'";
	$result2 = mysql_query($query2);					//查询结果集
	$result3 = mysql_fetch_array($result2);				//查询结果集取值
	$shijian = $result3["u_lastlogintime"];              //取出前一次登录时间
	
	$_SESSION["lastlogintime"] = $shijian;				//前一次登入时间记入$_SESSION["lastlogintime"];

	$_SESSION["ip"] = $ip;


	$sql = "update bbs_user set u_lastlogintime = '".$time."',u_lastloginip = '".$ip."' where u_name = '".$username."'";
	$result = mysql_query($sql);
	if($result)
	{
		$query = "select u_count from bbs_user where u_name = '".$username."'";
		$login_times = mysql_query($query); //查询结果
		$counts= mysql_fetch_array($login_times); //取查询结果
		$counts["u_count"] = $counts["u_count"]+1; //登录次数+1
		$cishu = $counts["u_count"];
		$_SESSION["login_count"] = $cishu;
		$query = "update bbs_user set u_count = '".$cishu."' where u_name = '".$username."'";
		mysql_query($query);
		echo "<script>window.location.href='index.php';</script>";

	}
	else {
		echo "result错了";
	}
}
else {
	echo "<script>alert('用户名或密码错误或不存在');history.back();</script>";
}


?>